package com.cn.kehong.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.kehong.api.IProductPackageService;
import com.cn.kehong.domain.ProductPackage;
import com.cn.kehong.domain.ProductPackageJob;
import com.cn.kehong.domain.ProductPackageVo;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.JsonResponse;
import com.cn.kehong.utils.QueryForm;

import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/productPackage")
public class ProductPackageController {

    @Autowired
    private IProductPackageService iProductPackageService;

    //车间包装列表页面
    @RequestMapping("/getProductPackageList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductPackageVo>> getProductPackageList(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<List<ProductPackageVo>> response = new JsonResponse<>();
        List<ProductPackageVo> productPackageVos = iProductPackageService.getProductPackageList(queryForm);
        response.setResult(productPackageVos);
        response.setSuccess(true);
        return response;
    }


    //包装明细列表页面
    @RequestMapping("/getPackageListByType/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductPackageVo>> getPackageListByType(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<List<ProductPackageVo>> response = new JsonResponse<>();
        List<ProductPackageVo> productPackageVos = iProductPackageService.getPackageListByType(queryForm);
        response.setResult(productPackageVos);
        response.setSuccess(true);
        return response;
    }

    //包装下载接口
    @ResponseBody
    @RequestMapping("/exportProductPackage")
    public void exportProductPackage(String queryStr, HttpServletResponse response) throws Exception {
        QueryForm queryForm = new QueryForm();
        if(StringUtils.isNotEmpty(queryStr)){
            queryStr = URLDecoder.decode(queryStr,"utf-8");
            queryStr = new String(queryStr.getBytes("ISO-8859-1"),"utf-8");
            JSONObject jsonObject = JSON.parseObject(queryStr);
            queryForm.setMaterialName(jsonObject.getString("materialName"));
            queryForm.setProductCode(jsonObject.getString("productCode"));
            queryForm.setFactoryName(jsonObject.getString("factoryName"));
            queryForm.setPackageRoom(jsonObject.getString("packageRoom"));
            queryForm.setBanCi(jsonObject.getString("banCi"));
            queryForm.setLaborTime(jsonObject.getString("labelType"));
            queryForm.setStartTime(jsonObject.getString("startTime"));
            queryForm.setEndTime(jsonObject.getString("endTime"));
            queryForm.setStatus(jsonObject.getString("status"));
            queryForm.setName(jsonObject.getString("name"));
            queryForm.setQueryTime(jsonObject.getString("queryTime"));
            queryForm.setStartTime(jsonObject.getString("startTime"));
            queryForm.setEndTime(jsonObject.getString("endTime"));
        }
        iProductPackageService.exportProductPackage(queryForm,response);
    }


    //通过生产批次来查询包装ProductPackage信息
    @RequestMapping("/getPackageList/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getPackageList(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object>  packageList = iProductPackageService.getPackageList(queryForm);
        response.setResult(packageList);
        response.setSuccess(true);
        return response;
    }

    //包装应用
    @RequestMapping("/getProductPackageByRoom/{frontToken}")
    @ResponseBody
    public JsonResponse<ProductPackage> getProductPackageByRoom(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<ProductPackage> response = new JsonResponse<ProductPackage>();
        ProductPackage productPackage = iProductPackageService.getProductPackageByRoom(queryForm);
        response.setResult(productPackage);
        response.setSuccess(true);
        return response;
    }

    @RequestMapping("/addProductPackage/{frontToken}")
    @ResponseBody
    public JsonResponse<String> addProductPackage(ProductPackage productPackage, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = iProductPackageService.addProductPackage(productPackage);
        return CommonUtil.getResponseData(response,result);
    }


    @RequestMapping("/addBatchProductPackageJob/{frontToken}")
    @ResponseBody
    public JsonResponse<String> addBatchProductPackageJob(@RequestBody List<ProductPackageJob> productPackageJobs, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = iProductPackageService.addBatchProductPackageJob(productPackageJobs);
        return CommonUtil.getResponseData(response,result);
    }


    //分级包装房
    @RequestMapping("/getProductPackageListByStatus/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductPackage>> getProductPackageListByStatus(@PathVariable(value = "frontToken", required = true) String frontToken,String factoryName) throws Exception {
        JsonResponse<List<ProductPackage>> response = new JsonResponse<>();
        List<ProductPackage> productPackages = iProductPackageService.getProductPackageListByStatus(factoryName);
        response.setResult(productPackages);
        response.setSuccess(true);
        return response;
    }

    //通过product_code来查询
    @ResponseBody
    @RequestMapping("/getPackageJobList/{frontToken}")
    public JsonResponse<Map<String,Object>> getPackageJobList(@RequestBody List<ProductPackage> productPackages, @PathVariable(value = "frontToken", required = true) String frontToken) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object>  packageList = iProductPackageService.getPackageJobList(productPackages);
        response.setResult(packageList);
        response.setSuccess(true);
        return response;
    }

    //批次关系列表页面
    @RequestMapping("/getPackageBatchList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductPackage>> getPackageBatchList(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<List<ProductPackage>> response = new JsonResponse<>();
        List<ProductPackage> packageList = iProductPackageService.getPackageBatchList(queryForm);
        response.setResult(packageList);
        response.setSuccess(true);
        return response;
    }

    //批次追溯过程下载接口
    @ResponseBody
    @RequestMapping("/exportBatch")
    public void exportBatch(String queryStr, HttpServletResponse response) throws Exception {
        QueryForm queryForm = new QueryForm();
        if(StringUtils.isNotEmpty(queryStr)){
            queryStr = URLDecoder.decode(queryStr,"utf-8");
            queryStr = new String(queryStr.getBytes("ISO-8859-1"),"utf-8");
            JSONObject jsonObject = JSON.parseObject(queryStr);
            queryForm.setProductCode(jsonObject.getString("productCode"));
        }
        iProductPackageService.exportBatch(queryForm,response);
    }


    //批次追溯批次关系下载接口
    @ResponseBody
    @RequestMapping("/exportRelationship")
    public void exportRelationship(String queryStr1, HttpServletResponse response) throws Exception {
        QueryForm queryForm = new QueryForm();
        if(StringUtils.isNotEmpty(queryStr1)){
            queryStr1 = URLDecoder.decode(queryStr1,"utf-8");
            queryStr1 = new String(queryStr1.getBytes("ISO-8859-1"),"utf-8");
            JSONObject jsonObject = JSON.parseObject(queryStr1);
            queryForm.setProductCode(jsonObject.getString("productCode"));
        }
        iProductPackageService.exportRelationship(queryForm,response);
    }

    //包装
    @RequestMapping("/getProductPackageJobsById/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductPackageJob>> getProductPackageJobsById(@PathVariable(value = "frontToken", required = true) String frontToken,String packageId) throws Exception {
        JsonResponse<List<ProductPackageJob>> response = new JsonResponse<>();
        List<ProductPackageJob> productPackages = iProductPackageService.getProductPackageJobsById(packageId);
        response.setResult(productPackages);
        response.setSuccess(true);
        return response;
    }

    //锁定包装
    @RequestMapping("/updateLockPackageJob/{frontToken}")
    @ResponseBody
    public JsonResponse<String> updateLockPackageJob(ProductPackageJob productPackageJob, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = iProductPackageService.updateLockPackageJob(productPackageJob);
        return CommonUtil.getResponseData(response,result);
    }
    @RequestMapping("/updateCompletePackageJob/{frontToken}")
    @ResponseBody
    public JsonResponse<String> updateCompletePackageJob(ProductPackageJob productPackageJob, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = iProductPackageService.updateCompletePackageJob(productPackageJob);
        return CommonUtil.getResponseData(response,result);
    }

    //分级结束包装
    @RequestMapping("/updateFenjiPackage/{frontToken}")
    @ResponseBody
    public JsonResponse<String> updateFenjiPackage(String id, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = iProductPackageService.updateFenjiPackage(id);
        return CommonUtil.getResponseData(response,result);
    }

    @RequestMapping("/updateEndPackage/{frontToken}")
    @ResponseBody
    public JsonResponse<String> updateEndPackage(String id, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = iProductPackageService.updateEndPackage(id);
        return CommonUtil.getResponseData(response,result);
    }


    @RequestMapping("/getProductPackageRecentOneDay/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductPackage>> getProductPackageRecentOneDay(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<List<ProductPackage>> response = new JsonResponse<>();
        List<ProductPackage> productPackages = iProductPackageService.getProductPackageRecentOneDay(queryForm);
        response.setResult(productPackages);
        response.setSuccess(true);
        return response;
    }

    @RequestMapping("/printProductPackageJob/{frontToken}")
    @ResponseBody
    public JsonResponse<String> printProductPackageJob(@PathVariable(value = "frontToken", required = true) String frontToken, String jobId) throws Exception {
        JsonResponse<String> response = new JsonResponse<>();
        String result = iProductPackageService.printProductPackageJob(jobId);
        return CommonUtil.getResponseData(response,result);
    }

    @RequestMapping("/printProductSample/{frontToken}")
    @ResponseBody
    public JsonResponse<String> printProductSample(@PathVariable(value = "frontToken", required = true) String frontToken, String jobId) throws Exception {
        JsonResponse<String> response = new JsonResponse<>();
        String result = iProductPackageService.printProductSample(jobId);
        return CommonUtil.getResponseData(response,result);
    }

    //当前在产
    @RequestMapping("/getCurrentProductPackageList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductPackage>> getCurrentProductPackageList(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<List<ProductPackage>> response = new JsonResponse<>();
        List<ProductPackage> productPackages = iProductPackageService.getCurrentProductPackageList(queryForm);
        response.setResult(productPackages);
        response.setSuccess(true);
        return response;
    }


    //包装明细查看
    @RequestMapping("/getProductPackageDetail/{frontToken}")
    @ResponseBody
    public JsonResponse<ProductPackage> getProductPackageDetail(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<ProductPackage> response = new JsonResponse<ProductPackage>();
        ProductPackage productPackage = iProductPackageService.getProductPackageDetail(queryForm);
        response.setResult(productPackage);
        response.setSuccess(true);
        return response;
    }


    //首页
    @RequestMapping("/getTodayPackageInfo/{frontToken}")
    @ResponseBody
    public JsonResponse<List<JSONObject>> getTodayPackageInfo(@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<JSONObject> > response = new JsonResponse<List<JSONObject>>();
        List<JSONObject> result = iProductPackageService.getTodayPackageInfo();
        response.setResult(result);
        response.setSuccess(true);
        return response;
    }
}
